<template>
  <n-card :header-style="{ padding: '10px', background: '#F1F5FF', marginBottom: '10px' }" ref="elementRef"
    class="schedule-price-component">
    <template #header> {{ index }}.排期与出价</template>
    <n-skeleton :repeat="8" text v-if="schedulePrice.loading"></n-skeleton>
    <x-form v-show="!schedulePrice.loading" ref="formRef" v-model:model="schedulePrice" :rules="rules"
      label-placement="top" label-width="120">
      <n-form-item label="投放日期" path="releaseDateType" :show-require-mark="true">
        <div class="flex-1">
          <n-radio-group v-model:value="schedulePrice.releaseDateType" @update:value="changeReleaseDateType">
            <n-radio-button v-for="item in dictionary['ENG_RELEASE_DATE_TYPE']" :key="item.value" :value="item.value"
              :label="item.label" />
          </n-radio-group>
          <div class="mt-10" style="width: 240px" v-if="schedulePrice.releaseDateType === 5">
            <n-date-picker type="daterange" v-model:value="schedulePrice.releaseDate"
              @update:value="changeReleaseBeginAndEnd" v-if="schedulePrice.releaseDateType === 5"
              :is-date-disabled="releaseDateRangeDisabled"></n-date-picker>
          </div>
        </div>
      </n-form-item>
      <n-form-item label="投放时间" :show-feedback="false" :show-require-mark="true">
        <n-form-item :show-label="false">
          <n-radio-group v-model:value="schedulePrice.releaseTimeType" @update:value="checkTimeType">
            <n-radio-button v-for="item in dictionary['ENG_RELEASE_TIME_TYPE']" :key="item.value" :value="item.value"
              :label="item.label" />
          </n-radio-group>
        </n-form-item>
        <n-form-item v-if="schedulePrice.releaseTimeType === 2" style="margin-left: 10px; width: 280px"
          :show-label="false" path="releaseTimeType">
          <n-select v-model:value="schedulePrice.releaseBeginTime" style="width: 120px" :options="optionsBeginTime"
            @update:value="checkReleaseBeginTIme" />
          <span style="margin: 0 8px">至</span>
          <n-select v-model:value="schedulePrice.releaseEndTime" style="width: 120px" :options="optionsEndTime" />
        </n-form-item>
      </n-form-item>
      <n-form-item label="首日投放时间">

        <div class="flex-1">
          <n-switch v-model:value="schedulePrice.openFirstDayBeginTime"
            @update:value="schedulePrice.firstDayBeginTime === undefined"></n-switch>
          <div v-if="schedulePrice.openFirstDayBeginTime">
            <n-select class="mt-5" v-model:value="schedulePrice.firstDayBeginTime" style="width: 120px;"
              :options="firstPutoptionsTime" placeholder="首日投放时间" clearable />
            <div class="text-helper mt-5">投放时间段必须包含指定的首日开始时间</div>
          </div>
        </div>

      </n-form-item>
      <n-form-item label="出价方式" path="bidMode" :show-require-mark="true">
        <n-radio-group v-model:value="schedulePrice.bidMode" @update:value="changeBidMode">
          <n-radio-button :key="1" value="BID_MODE_OCPM" label="oCPM" />
          <n-radio-button :key="2" value="BID_MODE_CPM" label="CPM" />

        </n-radio-group>
      </n-form-item>
      <n-form-item label="出价类型" path="bidScene" :show-require-mark="true"
        v-if="schedulePrice.bidMode === 'BID_MODE_OCPM'">
        <n-radio-group v-model:value="schedulePrice.bidScene" @update:value="changeBidScene">
          <n-radio-button :key="1" value="SMART_BID_TYPE_CUSTOM" label="稳定拿量" />
          <n-radio-button :key="2" value="SMART_BID_TYPE_SYSTEMATIC" label="最大转化量投放" />
        </n-radio-group>
      </n-form-item>
      <n-form-item label="优化目标" path="optimizationGoal" :show-require-mark="true"
        v-if="schedulePrice.bidMode === 'BID_MODE_OCPM'">
        <n-radio-group v-model:value="schedulePrice.optimizationGoal">
          <n-radio-button :key="1" value="OPTIMIZATIONGOAL_ECOMMERCE_ORDER" label="下单" />
        </n-radio-group>
      </n-form-item>
      <!-- 使 深度优化方式 默认 深度优化目标-->
      <n-form-item label="深度优化方式" path="deepConversionType" :show-require-mark="true"
        v-if="schedulePrice.bidMode === 'BID_MODE_OCPM' && !adv.market.isWxChannelsLive">
        <n-radio-group v-model:value="schedulePrice.deepConversionType" @update:value="changeDeepConversionType">
          <n-radio-button :key="1" :value="1" label="深度优化目标" />
          <n-radio-button :key="1" :value="2" label="深度辅助优化" />
        </n-radio-group>
        <n-radio-group class="ml-10" v-model:value="schedulePrice.deepOptimalWarn"
          v-if="schedulePrice.deepConversionType">
          <n-radio :value="1" label="上传失败"></n-radio>
          <n-radio :value="2" label="深度转化优化关闭"></n-radio>
        </n-radio-group>

      </n-form-item>
      <!-- <n-form-item v-if="schedulePrice.deepConversionType" label="原深度辅助roi未开白" path="deepOptimalWarn"
        :show-require-mark="true">
          <n-radio-group v-model:value="schedulePrice.deepOptimalWarn">
            <n-radio-button :key="1" :value="1" label="上传失败" />
            <n-radio-button :key="2" :value="2" label="深度转化优化关闭" />
          </n-radio-group>

      </n-form-item> -->

      <n-form-item v-if="schedulePrice.bidMode === 'BID_MODE_OCPM'" label="深度转化类型" path="deepConversionGoal"
        :show-require-mark="true">
        <!-- DEEP_CONVERSION_WORTH | DEEP_CONVERSION_WORTH_ADVANCED -->
        <n-button-group :value="schedulePrice.deepConversionGoal">
          <n-button ghost :type="item.value == schedulePrice.deepConversionGoal ? 'info' : undefined"
            @click.prevent="changeDeepConversionGoal(item.value)" :focusable="false"
            v-for="item in deepConversionGoalOptional" :key="item.value" :value="item.value">{{ item.label
            }}</n-button>
        </n-button-group>
        <!-- schedulePrice.deepConversionGoal: {{ schedulePrice.deepConversionGoal }} -->

        <!-- <n-radio-group :value="schedulePrice.deepConversionGoal" @update:value="changeDeepConversionGoal">
          <n-radio-button
            v-for="item in deepConversionGoalOptional" :key="item.value" :value="item.value" :label="item.label"></n-radio-button>
        </n-radio-group> -->


      </n-form-item>

      <!-- 稳定拿量的场景下(schedulePrice.bidScene == 'SMART_BID_TYPE_CUSTOM') -->
      <!-- ocpm广告要求至少需要 2 倍于广告出价的 广告日预算 才能进行投放 -->
      <!-- 【1 付费】 【2/3 下单ROI 下单ROI】 -->
      <!-- 只有下单ROI 下单ROI 原深度辅助优化 才有-->

      <n-form-item label="期望ROI" path="deepConversionExpectedRoi"
        v-if="schedulePrice.bidScene == 'SMART_BID_TYPE_CUSTOM' && schedulePrice.deepConversionGoal == 2 || schedulePrice.deepConversionGoal == 3">
        <n-input-number v-model:value="schedulePrice.deepConversionExpectedRoi" style="width: 450px" :min="0.001"
          :max="50" placeholder="期望ROI目标范围0.001~50，输入0.05，表示ROI目标为5%" :show-button="false" />
      </n-form-item>
      <!-- 付费的时候 -->
      <n-form-item label="深度目标出价" path="deepConversionExpectedRoi"
        v-if="schedulePrice.bidScene == 'SMART_BID_TYPE_CUSTOM' && schedulePrice.deepConversionGoal == 1">
        <n-input-number v-model:value="schedulePrice.deepConversionExpectedRoi" style="width: 450px" :min="0.1"
          :max="2000" :precision="3" placeholder="深度目标出价”范围是0.1~20000" :show-button="false" />
      </n-form-item>
      <n-form-item label="出价" path="bidAmount"
        v-if="schedulePrice.bidScene === 'SMART_BID_TYPE_CUSTOM' || !schedulePrice.bidScene">
        <n-input-number v-model:value="schedulePrice.bidAmount" style="width: 400px;margin-right: 4px" :precision="2"
          :min="bidAMountRule.min" :max="bidAMountRule.max" :placeholder="bidAMountRule.placeholder"
          :show-button="false" />
        <n-text class="ml-10" depth="3">元/下单</n-text>

      </n-form-item>
      <!-- 只有ocpm可以一键启亮 -->
      <n-form-item label="一键起量" :show-feedback="false"
        v-if="schedulePrice.bidScene == 'SMART_BID_TYPE_CUSTOM' || schedulePrice.bidMode === 'BID_MODE_OCPM'">
        <n-form-item :show-label="false">
          <n-switch v-model:value="schedulePrice.autoAcquisitionEnabled" :checked-value="1" :unchecked-value="0" />
        </n-form-item>
        <n-form-item v-if="schedulePrice.autoAcquisitionEnabled" :show-label="false" path="autoAcquisitionBudget">
          <n-input-number v-model:value="schedulePrice.autoAcquisitionBudget" style="width: 450px; margin-left: 8px"
            :min="200" :max="10000000" :precision="2" placeholder="建议设置为出价的10倍,起量预算范围200~100000元，不能低于出价"
            :show-button="false" />
        </n-form-item>
      </n-form-item>

      <!-- ocpm广告要求至少需要 2 倍于广告出价的 广告日预算 才能进行投放 -->
      <n-form-item label="广告日预算" :show-require-mark="true" :show-feedback="false">
        <!-- 稳定拿量的场景下 -->
        <div class="flex-1">
          <n-form-item :show-label="false" style="margin-right: 8px" :show-feedback="schedulePrice.dailyBudget == 0">
            <div style="display: inline-block">
              <span :class="['_button-left cursor-pointer', schedulePrice.dailyBudget === 0 ? '_button-active' : '']"
                @click="changeDailyBudget('0')">不限</span>
              <span :class="['_button-right cursor-pointer', schedulePrice.dailyBudget !== 0 ? '_button-active' : '']"
                @click="changeDailyBudget('1')">指定日预算</span>
            </div>
          </n-form-item>
          <n-form-item v-if="schedulePrice.dailyBudget !== 0 || schedulePrice.bidScene == 'SMART_BID_TYPE_SYSTEMATIC'"
            :show-label="false" path="dailyBudget" style="margin-left: -8px;" class="mt-10">
            <div class="flex-1">
              <div class="flex items-center">
                <n-input-number v-model:value="schedulePrice.dailyBudget" style="margin-left: 8px;width: 400px;"
                  :min="dailyBudgetRule?.min" :max="40000000" :precision="2" placeholder="请输入广告日预算"
                  :show-button="false" />
                <n-text class="ml-10" depth="3">元/天</n-text>
              </div>
              <div class="text-helper ml-10 mt-2">每日预算 50-40,000,000元/天,ocpm广告至少需要2倍广告出价</div>
            </div>
          </n-form-item>
        </div>
      </n-form-item>

      <n-form-item label="广告名称" :rule="adgroupNameRules" path="none" :show-require-mark="true">
        <div class="flex-1">
          <n-input v-model:value="adv.adgroupName" type="text" :maxlength="advNameMaxLength" show-count
            :count-graphemes="count" ref="nInputRef" placeholder="请输入广告名称" @keydown.delete="deleteName" />

          <div class="cursor-pointer flex between items-center mt-10" style="max-width: 480px;;">
            <div>
              <span class="text-helper">通配符: </span>
              <span @click="addQuickName(item)" class="text-helper link-color ml-5" name="[]"
                v-for="(item) in quickNameText.list.slice(0, 2)">+{{
                  item.name
                }}</span>
            </div>
            <div>
              <n-select placeholder="插入更多词包" size="tiny" style="width: 160px" :options="quickNameText.list"
                label-field="name" value-field="id" @update:value="(_, item) => addQuickName(item)"></n-select>
            </div>

          </div>
        </div>


      </n-form-item>
    </x-form>
  </n-card>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
const router = useRouter()
import { XForm, XButton } from '@/components'
import { count } from '@/utils/text';
import {
  NCard,
  NText,
  NFormItem,
  NSwitch,
  NDatePicker,
  NInputNumber,
  NInput,
  NRadioGroup,
  NRadioButton,
  NSelect,
  NDataTable,
  NButtonGroup,
  NButton,
  useMessage,
  NSkeleton,
  NRadio
} from 'naive-ui'
import { computed, onMounted, reactive, ref, watch, h, nextTick } from 'vue'
import { useStore } from '@/store'
import { formatZero, parseTime, formatDate } from '@/utils'
import { useDictionary } from '@/hooks';
import { getRealAdvName } from '@/store/modules/adv/helper';


const message = useMessage()
const store = useStore()
const props = defineProps({
  index: {
    type: Number
  },
  name: {
    type: String,
    default: ''
  }
})
const getProps = reactive(props)
const adv = computed(() => store.state.adv.modal);
const schedulePrice = computed(() => store.state.adv.modal.schedulePrice);
const diect = computed(() => store.state.adv.modal.diect);
const position = computed(() => store.state.adv.modal.position)
const advNameMaxLength = ref(56);
const deepConversionGoalOptional = computed(() => {
  // 优量汇(1) + 腾讯平台与内容媒体(15) 付费只支持
  let siteSet: string = adv.value.position.siteSet.join(',');
  // 去除搜索场景
  siteSet = siteSet.replace(',122', '');


  if (siteSet === '100,101,27,28,15' || siteSet === '15,100,101,27,28' || siteSet === '100,101,27,28' || siteSet === '15') {
    const deepConversionType = schedulePrice.value.deepConversionType;
    // 深度辅助优化 deepConversionType == 1;
    return [
      {
        label: '不开启',
        value: undefined,
      },
      {
        label: '付费',
        value: 1,
        show: deepConversionType == 1
      }, {
        label: '下单ROI',
        value: 2,
        show: true
      }, {
        label: '下单ROI 原深度辅助优化',
        value: 3,
        show: deepConversionType == 1
      }].filter(_ => _.show)
  } else {
    return [
      {
        label: '不开启',
        value: undefined,
      },
      {
        label: '下单ROI',
        value: 2
      }, {
        label: '下单ROI 原深度辅助优化',
        value: 3
      }]

  }



})

const nInputRef = ref();
function deleteName(e) {
  e.preventDefault();
  let name = adv.value.adgroupName;
  // console.log(e, nInputRef)
  const cursorPosition = e.target.selectionStart;
  const endPosition = e.target.selectionEnd;
  const leftPart = name.substring(0, cursorPosition);
  const rightPart = name.substring(endPosition);
  if (cursorPosition != endPosition) {
    name = leftPart + rightPart;
    adv.value.adgroupName = name;
    e.target.value = name;
    e.target.setSelectionRange(cursorPosition, cursorPosition);

    e.preventDefault();
    return;
  }

  // 向左截取当前光标位置之前的字符串
  // const leftPart = inputString.substring(0, cursorPosition);

  const match = leftPart.match(/{{[^}]*}}$/);
  if (match) {
    // 找到匹配项，删除 {{...}} 结构
    const newCursorPosition = cursorPosition - match[0].length;
    name = leftPart.slice(0, -match[0].length) + name.substring(cursorPosition);

    // 设置新的字符串到输入框
    adv.value.adgroupName = name;
    e.target.value = name;

    // 设置光标位置到删除后的正确位置
    e.target.setSelectionRange(newCursorPosition, newCursorPosition);

    // 阻止默认的删除操作
    // event.preventDefault();
  } else {
    name = leftPart.slice(0, -1) + name.substring(cursorPosition);

    // 设置新的字符串到输入框
    adv.value.adgroupName = name;
    e.target.value = name;

    // 设置光标位置到删除后的正确位置
    const newCursorPosition = cursorPosition - 1;
    e.target.setSelectionRange(newCursorPosition, newCursorPosition);
  }
  // if()

}

const quickNameText = computed<{ list: any }>(() => {
  return {
    list: [
      //   {
      //   name: '素材名',
      //   id: '素材名',
      // },
      {
        name: '账户ID',
        id: '账户ID'
      }, {
        name: '日期',
        id: '日期'
      }, {
        name: '时分秒',
        id: '时分秒'
      }, {
        name: '版位名称',
        id: '版位名称'
      },
      diect.value.outTargetingName ? {
        name: '定向包名称',
        id: '定向包名称'
      } : null].filter(_ => _)
  }
});

const dailyBudgetRule = computed(() => {
  const bidMode = schedulePrice.value.bidMode;
  if (bidMode === 'BID_MODE_OCPM') return {
    max: 40000000,
    // placeholder: '出价超出范围0.1~5000元',
    min: schedulePrice.value.dailyBudget < 50 ? 50 : schedulePrice.value.bidAmount * 2
  }
  if (bidMode === 'BID_MODE_CPM') return {
    min: 50,
    // placeholder: '出价超出范围1.5~999元',
    max: 40000000
  }
})

const bidAMountRule = computed(() => {
  const bidMode = schedulePrice.value.bidMode;
  if (bidMode === 'BID_MODE_CPM') return {
    min: 1.5,
    placeholder: '出价超出范围1.5~999元',
    max: 999
  }
  if (bidMode === 'BID_MODE_OCPM') return {
    max: 5000,
    placeholder: '出价超出范围0.1~5000元',
    min: 0.1
  }
  return {
    max: 999,
    min: 1.5
  }
})

function switchDeepConversionGoal(v) {
  if (schedulePrice.value.deepConversionGoal) {
    schedulePrice.value.deepConversionGoal = undefined;

  }
  // schedulePrice.value.deepConversionGoal = schedulePrice.value.deepConversionGoal ? undefined :
}

function addQuickName(item: any) {

  const text = adv.value.adgroupName + (adv.value.adgroupName ? `_{{${item.id}}}` : `{{${item.id}}}`);
  if (text.length >= advNameMaxLength.value) {
    message.warning(`广告名称不能超过${advNameMaxLength.value}字`);
    return;
  }

  adv.value.adgroupName = text;
}
const releaseDateRangeDisabled = ref((ts: any, phase: string, value: any) => {
  // const [start, end] = value || [];
  const _now = new Date();
  const date = _now.getDate();
  const month = _now.getMonth();
  const year = _now.getFullYear()
  return ts < Date.now() - 86400000 || ts < Number(new Date(year - 1, month, date)) || ts > Number(new Date(year + 1, month, date));
})

function changeBidScene(value: any) {
  if (value == 'SMART_BID_TYPE_CUSTOM') {
    schedulePrice.value.dailyBudget = undefined;
  }
  if (value == 'SMART_BID_TYPE_SYSTEMATIC') {
    schedulePrice.value.bidAmount = undefined;
    // 一键启亮关闭
    schedulePrice.value.autoAcquisitionEnabled = undefined;
    //  一键启亮金额关闭
    schedulePrice.value.autoAcquisitionBudget = undefined;
    schedulePrice.value.deepConversionExpectedRoi = undefined;
  }
}
function changeDeepConversionType(_: any) {
  schedulePrice.value.deepConversionGoal = undefined;
}

function changeDeepConversionGoal(v: any) {
  // const deepConversionGoal = schedulePrice.value.deepConversionGoal;

  // if (deepConversionGoal == v) {
  //   schedulePrice.value.deepConversionGoal = undefined;
  // } else {
  //   schedulePrice.value.deepConversionGoal = v;
  // }

  schedulePrice.value.deepConversionGoal = v;

  // console.log('change');
  // 期望roi关闭
  schedulePrice.value.deepConversionExpectedRoi = undefined;
}

function changeBidMode(value: any) {
  if (value == 'BID_MODE_OCPM') {
    schedulePrice.value.optimizationGoal = 'OPTIMIZATIONGOAL_ECOMMERCE_ORDER';
    schedulePrice.value.deepConversionType = 1;
    schedulePrice.value.bidScene = 'SMART_BID_TYPE_CUSTOM';
    schedulePrice.value.deepConversionGoal = undefined;
  }
  if (value == 'BID_MODE_CPM') {
    // 优化目标关闭
    schedulePrice.value.optimizationGoal = undefined;
    // 深度优化类型关闭
    schedulePrice.value.deepConversionGoal = undefined;
    // 出价类型关闭
    schedulePrice.value.bidScene = undefined;
    // 深度优化方式关闭
    schedulePrice.value.deepConversionType = undefined;
    // 期望roi关闭
    schedulePrice.value.deepConversionExpectedRoi = undefined;
    // 一键启亮关闭
    schedulePrice.value.autoAcquisitionEnabled = undefined;
    //  一键启亮金额关闭
    schedulePrice.value.autoAcquisitionBudget = undefined;
  }
}


function changeReleaseDateType(key: any) {
  const _now = new Date();
  const date = _now.getDate();
  const month = _now.getMonth();
  const year = _now.getFullYear()

  schedulePrice.value.releaseDate = [Number(_now), Number(new Date(year + 1, month, date))];

}


function changeReleaseBeginAndEnd(v: any) {
  if (!v[0]) return;
  schedulePrice.value.releaseBeginDate = formatDate(v[0]);
  schedulePrice.value.releaseEndDate = formatDate(v[1]);
}


const { get: getDic } = useDictionary()
const dictionary = computed(() => store.getters.dictionary)

const range = []
for (let i = 0; i <= 48; i++) {
  let hour: any = i % 2 == 0 ? i / 2 : (i - 1) / 2;
  hour = hour < 10 ? '0' + hour : hour;
  const text = i % 2 == 0 ? `${hour}:00` : `${hour}:30`;
  range.push({
    label: text,
    value: text,
    disabled: false
  })
}
const range1 = []
for (let i = 0; i <= 48; i++) {
  let hour: any = i % 2 == 0 ? i / 2 : (i - 1) / 2;
  hour = hour < 10 ? '0' + hour : hour;
  const text = i % 2 == 0 ? `${hour}:00` : `${hour}:30`;
  range1.push({
    label: text,
    value: text,
    disabled: false
  })
}

const range3: any = [];
for (let i = 1; i <= 24; i++) {
  range3.push({
    label: i < 10 ? `0${i}:00` : `${i}:00`,
    value: i < 10 ? `0${i}:00` : `${i}:00`,
    number: +(i + '00'),
    disabled: false
  })
}

const firstPutoptionsTime = computed(() => {
  const releaseBeginTime = schedulePrice.value.releaseBeginTime;
  const releaseEndTime = schedulePrice.value.releaseEndTime;
  const beginValue = +((releaseBeginTime || '').replace(":", '')) || 0;
  const endValue = +((releaseEndTime || '').replace(":", '')) || 2400;

  return range3.filter((_: any) => {
    return _.number >= beginValue && _.number <= endValue
  })

})

const optionsBeginTime = ref(range)
const optionsEndTime = ref(range1);
// const firstPutoptionsTime = ref(range3);

const adgroupNameRules = ref({
  trigger: 'input',
  validator(rule: any, value: any) {
    if (!adv.value.adgroupName) {
      return new Error('请填写广告名称!')
    }
    return true;

  }
})
const rules = reactive({
  dateTypeDate: {
    type: 'string',
    required: true,
    trigger: ['change'],
    message: '请选择开始时间'
  },
  dateTypeDateRange: {
    type: 'array',
    required: true,
    trigger: ['change'],
    message: '请指定日期和结束日期'
  },
  bidAmount: {
    type: 'number',
    required: true,
    trigger: ['input', 'blur'],
    message: '请输入出价'
  },
  // deepConversionGoal: {
  //   type: 'number',
  //   required: true,
  //   trigger: ['input', 'blur'],
  //   message: '深度转化类型必填写'

  // },
  autoAcquisitionBudget: {
    type: 'number',
    required: true,
    trigger: ['input', 'blur'],
    message: '请输入一键起量范围'
  },
  deepConversionExpectedRoi: {
    type: 'number',
    required: true,
    trigger: ['input', 'blur'],
    validator(_: any, value: any) {
      if (!value) {
        return new Error(schedulePrice.value.deepConversionGoal != 1 ? '请输入期望ROI目标范围!' : '请输入深度目标出价!')
      }
      return true;
    },
    // message: '请输入期望ROI目标范围'
  },
  dailyBudget: {
    type: 'number',
    required: true,
    trigger: ['input', 'blur'],
    message: '每日预算50-40,000,000元/天'
  },
  name: {
    type: 'string',
    required: true,
    trigger: ['input', 'blur'],
    // message: '请输入广告名称',
    validator(rule: any, value: any) {
      if (!value) {
        return new Error(`请输入广告名称!`);
      }
      if (value.length > advNameMaxLength.value) {
        return new Error(`广告名称长度不能超过${advNameMaxLength.value}字!`);
      }
      return true;

    }
  }
})

const formRef = ref()

onMounted(() => {
  getDic(
    'IMMEDIATE,ENG_DEEP_CONVERSION_WORTH_GOAL,ENG_DEEP_CONVERSION_TYPE,ENG_BID_STRATEGY,ENG_SMART_BID_TYPE,ENG_OPTIMIZATION_GOAL,ENG_BID_MODE,ENG_RELEASE_TIME_TYPE,ENG_RELEASE_DATE_TYPE'
  )
})
function changeDailyBudget(type: any) {
  if (+type === +0) {
    schedulePrice.value.dailyBudget = 0
  } else {
    schedulePrice.value.dailyBudget = null
  }
}
function checkReleaseBeginTIme(val: number, option: any) {
  optionsEndTime.value.forEach((item: any) => {
    item.disabled = item.value <= val
  });
  schedulePrice.value.firstDayBeginTime = undefined;
  schedulePrice.value.releaseEndTime = null
}

// 投放时间切换
function checkTimeType(val: any) {
  schedulePrice.value.releaseBeginTime = undefined
  schedulePrice.value.releaseEndTime = undefined;
}
const elementRef = ref();

/*
 * 表单数据校验
 * */
function validate() {

  const name = getRealAdvName(store.state.adv);

  adv.value.adgroupName = name;
  return new Promise<void>((resolve, reject) => {
    formRef.value?.validate(async (errors: any) => {
      if (!errors) {
        try {


          resolve()
        } catch (e) {
          reject()
        }
      } else {
        store.commit('adv/SCROLL_TO_ELEMENT', elementRef.value.$el)
        reject()
        // message.warning('请正确填写排期与出价下的信息');
      }
    })
  })
}

defineExpose({
  validate
})
</script>
<style lang="scss">
.schedule-price-component {
  .n-button:not(.n-button--disabled):hover {
    background-color: rgba(0, 0, 0, 0) !important;
  }

  .n-button:not(.n-button--disabled):hover .n-button__state-border {
    border: rgba(0, 0, 0, 0) !important;
  }
}
</style>
<style scoped lang="scss">
@import '@/assets/styles/var';


._button-left {
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 4px 0px 0px 4px;
  border: 1px solid #dcdfe6;
}

._button-right {
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 0 4px 4px 0;
  border: 1px solid #dcdfe6;
}

._button-active {
  background-color: #ffffff;
  border-color: $primaryColor;
  color: $primaryColor;
}
</style>
